<template>
  <p>
    <a-space>
      <a-date-picker v-model:value="params.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
      <daily-train-select-view v-model:trainCodeValue="params.code" width="200px"></daily-train-select-view>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
      <a-button type="danger" @click="onClickGenDaily">手动生成车次信息</a-button>
    </a-space>
  </p>
  <a-table :dataSource="dailyTrains"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'type'">
        <span v-for="item in TRAIN_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.type">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <a-modal v-model:visible="visible" title="每日车次" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="dailyTrain" :label-col="{span: 4}" :wrapper-col="{ span: 20 }">
      <a-form-item label="日期">
        <a-date-picker v-model:value="dailyTrain.date" valueFormat="YYYY-MM-DD" placeholder="请选择日期" />
      </a-form-item>
      <a-form-item label="车次编号">
        <train-select-view v-model:trainCodeValue="dailyTrain.code" @change="onChangeCode"></train-select-view>
      </a-form-item>
      <a-form-item label="车次类型">
        <a-select v-model:value="dailyTrain.type">
          <a-select-option v-for="item in TRAIN_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="始发站">
        <station-select-view v-model:stationNameValue="dailyTrain.start"></station-select-view>
      </a-form-item>
      <a-form-item label="始发站拼音">
        <a-input v-model:value="dailyTrain.startPinyin" disabled/>
      </a-form-item>
      <a-form-item label="出发时间">
        <a-time-picker v-model:value="dailyTrain.startTime" valueFormat="HH:mm:ss" placeholder="请选择时间" />
      </a-form-item>
      <a-form-item label="终点站">
        <station-select-view v-model:stationNameValue="dailyTrain.end"></station-select-view>
      </a-form-item>
      <a-form-item label="终点站拼音">
        <a-input v-model:value="dailyTrain.endPinyin" disabled/>
      </a-form-item>
      <a-form-item label="到站时间">
        <a-time-picker v-model:value="dailyTrain.endTime" valueFormat="HH:mm:ss" placeholder="请选择时间" />
      </a-form-item>
    </a-form>
  </a-modal>

  <a-modal v-model:visible="genDailyVisible" title="生成车次" @ok="handleGenDailyOk"
           :confirm-loading="genDailyLoading" ok-text="确定" cancel-text="取消">
    <a-form :model="genDaily" :label-col="{span: 4}" :wrapper-col="{span: 20}">
      <a-form-item label="日期">
        <a-date-picker v-model:value="genDaily.date" placeholder="请选择日期"/>
      </a-form-item>
    </a-form>
  </a-modal>

</template>

<script>
import {defineComponent, ref, onMounted, watch} from 'vue';
import {notification} from "ant-design-vue";
import axios from "axios";
import StationSelectView from "@/components/StationSelect";
import {pinyin} from "pinyin-pro";
import dayjs from 'dayjs';
import DailyTrainSelectView from "@/components/DailytrainSelect";

export default defineComponent({
  name: "dailyTrainView",
  components: {DailyTrainSelectView, StationSelectView},
  setup() {
    const TRAIN_TYPE_ARRAY = window.TRAIN_TYPE_ARRAY;
    const visible = ref(false);
    let dailyTrain = ref({
      id: undefined,
      date: undefined,
      code: undefined,
      type: undefined,
      start: undefined,
      startPinyin: undefined,
      startTime: undefined,
      end: undefined,
      endPinyin: undefined,
      endTime: undefined,
      createTime: undefined,
      updateTime: undefined,
    });
    const dailyTrains = ref([]);
    // 分页的三个属性名是固定的
    const pagination = ref({
      total: 0,
      current: 1,
      pageSize: 10,
    });
    let loading = ref(false);
    let params = ref({
      code: null,
      date: null
    });
    const genDaily = ref({
      date: null
    });
    const genDailyVisible = ref(false);
    const genDailyLoading = ref(false);
    const columns = [
    {
      title: '日期',
      dataIndex: 'date',
      key: 'date',
    },
    {
      title: '车次编号',
      dataIndex: 'code',
      key: 'code',
    },
    {
      title: '车次类型',
      dataIndex: 'type',
      key: 'type',
    },
    {
      title: '始发站',
      dataIndex: 'start',
      key: 'start',
    },
    {
      title: '始发站拼音',
      dataIndex: 'startPinyin',
      key: 'startPinyin',
    },
    {
      title: '出发时间',
      dataIndex: 'startTime',
      key: 'startTime',
    },
    {
      title: '终点站',
      dataIndex: 'end',
      key: 'end',
    },
    {
      title: '终点站拼音',
      dataIndex: 'endPinyin',
      key: 'endPinyin',
    },
    {
      title: '到站时间',
      dataIndex: 'endTime',
      key: 'endTime',
    },
    {
      title: '操作',
      dataIndex: 'operation'
    }
    ];

    /**
     * 前端两个输入框同时pinyin插件 实时完成转换
     */
    watch(() => dailyTrain.value.start, () => {
      if(Tool.isNotEmpty(dailyTrain.value.start)) {
        dailyTrain.value.startPinyin = pinyin(dailyTrain.value.start, { toneType : 'none'}).replaceAll(" ","");
      } else {
        dailyTrain.value.startPinyin = "";
      }
    }, {immediate: true});

    watch(() => dailyTrain.value.end, () => {
      if(Tool.isNotEmpty(dailyTrain.value.end)) {
        dailyTrain.value.endPinyin = pinyin(dailyTrain.value.end, { toneType : 'none'}).replaceAll(" ","");
      } else {
        dailyTrain.value.endPinyin = "";
      }
    }, {immediate: true});

    const onAdd = () => {
      dailyTrain.value = {};
      visible.value = true;
    };

    const onChangeCode = (train) => {
      console.log("车次下拉组件选择:",train);
      //用js自带的方法 assign可以合并属性 把train里的内容和dailyTrain的同名字的属性进行赋值
      //assign(A,B)  B作为数据源 从B到A 如果B的id为null A的不管为什么只要有id字段 也会被赋值为null
      let temp = Tool.copy(train);
      delete temp.id;
      dailyTrain.value = Object.assign(dailyTrain.value, temp);
    };

    const onEdit = (record) => {
      dailyTrain.value = window.Tool.copy(record);
      visible.value = true;
    };

    const onDelete = (record) => {
      axios.delete("/business/admin/dailyTrain/delete/" + record.id).then((response) => {
        const data = response.data;
        if (data.success) {
          notification.success({description: "删除成功！"});
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize,
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const onClickGenDaily = () => {
      genDailyVisible.value = true;
    };

    const handleGenDailyOk = () => {
      // 校验日期是否为空或无效
      if (!genDaily.value.date || !dayjs(genDaily.value.date).isValid()) {
        notification.error({
          description: "请选择有效日期",
          duration: 2
        });
        return; // 直接返回，不执行后续请求
      }
      let date = dayjs(genDaily.value.date).format("YYYY-MM-DD");
      genDailyLoading.value = true;
      axios.get("/business/admin/dailyTrain/GenerateDaily/" + date).then((response) => {
        genDailyLoading.value = false;
        let data = response.data;
        if(data.success) {
          notification.success({description: "生成成功"});
          genDailyVisible.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        } else {
          notification.error({description: data.message});
        }
      })
    };

    const handleOk = () => {
      axios.post("/business/admin/dailyTrain/save", dailyTrain.value).then((response) => {
        let data = response.data;
        if (data.success) {
          notification.success({description: "保存成功！"});
          visible.value = false;
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleQuery = (param) => {
      if (!param) {
        param = {
          page: 1,
          size: pagination.value.pageSize
        };
      }
      loading.value = true;
      axios.get("/business/admin/dailyTrain/queryList", {
        params: {
          page: param.page,
          PageSize: param.size,
          code: params.value.code,
          date: params.value.date
        }
      }).then((response) => {
        loading.value = false;
        let data = response.data;
        if (data.success) {
          dailyTrains.value = data.content.list;
          // 设置分页控件的值
          pagination.value.current = param.page;
          pagination.value.total = data.content.total;
        } else {
          notification.error({description: data.message});
        }
      });
    };

    const handleTableChange = (page) => {
      // console.log("看看自带的分页参数都有啥：" + JSON.stringify(page));
      pagination.value.pageSize = page.pageSize;
      handleQuery({
        page: page.current,
        size: page.pageSize
      });
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      TRAIN_TYPE_ARRAY,
      dailyTrain,
      visible,
      dailyTrains,
      pagination,
      columns,
      handleTableChange,
      handleQuery,
      loading,
      onAdd,
      handleOk,
      onEdit,
      onDelete,
      onChangeCode,
      params,
      onClickGenDaily,
      genDaily,
      genDailyVisible,
      handleGenDailyOk,
      genDailyLoading
    };
  },
});
</script>
